package com.mozai.shopping.service.impl;

import com.mozai.shopping.dao.ActivityDao;
import com.mozai.shopping.model.Activity;
import com.mozai.shopping.model.vo.PageResp;
import com.mozai.shopping.service.ActivityService;
import com.mozai.shopping.utils.bean.ComonQueryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    ActivityDao activityDao;
    @Override
    public int insertActivity(Activity activity) {
        return activityDao.insert(activity);
    }

    @Override
    public int delActivity(Activity activity) {
        if (activityDao.selectByPrimaryKey(activity.getId()).getDelFlag()==0){
            activity.setDelFlag(1);
            return activityDao.updateByPrimaryKeySelective(activity);
        }
        return 0;
    }

    @Override
    public int modifyActivity(Activity activity) {
        if (activityDao.selectByPrimaryKey(activity.getId()).getDelFlag()==0){
            return activityDao.updateByPrimaryKeySelective(activity);
        }
        return 0;
    }

    @Override
    public PageResp<Activity> queryActivity(Activity activity, ComonQueryBean comonQueryBean) {
        //start
        if (comonQueryBean !=null && comonQueryBean.getPageSize() !=null &&comonQueryBean.getPageNum() !=null){
            comonQueryBean.setStart((comonQueryBean.getPageNum()-1)*comonQueryBean.getPageSize());
        }
        //List
        List<Activity> activityResult = activityDao.list4Page(activity,comonQueryBean);

        //total
        int count = activityDao.count(activity);
        comonQueryBean.setTotal(count);

        //totalPage
        comonQueryBean.setTotalPage(ComonQueryBean.calculateTotalPage(comonQueryBean));

        PageResp<Activity> activityPageResp =new PageResp<>();
        activityPageResp.setPageNum(comonQueryBean.getPageNum());
        activityPageResp.setPageSize(comonQueryBean.getPageSize());
        activityPageResp.setList(activityResult);
        activityPageResp.setStart(comonQueryBean.getStart());
        activityPageResp.setTotal(comonQueryBean.getTotal());
        activityPageResp.setTotalPage(comonQueryBean.getTotalPage());
        return activityPageResp;

    }
}
